"use strict";
/**
 * @author wangchenzhao
 * @date 2018/11/21
 */
const Controller = require('egg').Controller;

class dbcontroller extends Controller {

  /**
   * this赋值 便捷处理
   * @param param
   */
  constructor(param) {
    super(param);
    this.mysql = this.app.database.exam_mysql;
  }


  async home() {
    await this.ctx.render('/h5/index');
    // await this.ctx.render('/index.html');
  }
  async toHtml() {
    await this.ctx.render(`/h5${this.ctx.path}`);
    // await this.ctx.render('/index.html');
  }
  async getDBlist() {
    const { ctx, service } = this;
    let results = await service.mysqlTable.getAllDataBses();
    // ctx.body={results}
    let newArr = []
    if (results.length > 0) {
      let i = 0
      for (const iterator of results) {
        newArr.push({
          id: i,
          database: iterator.Database
        })

        i++;
      }
    }
    // console.log(newArr)
    await this.ctx.render(`/h5${this.ctx.path}`, { newArr });
  }

  async getTablesByDataBase() {
    const { ctx, service } = this;
    let db_name = ctx.query.db_name;

    let table_list = await service.mysqlTable.getTablesByDataBase(db_name);
    let db_list = await service.mysqlTable.getAllDataBses();
    // ctx.body = results;
    await this.ctx.render(`/h5${this.ctx.path}`, { table_list: table_list, db_list: db_list });
  }

  async getOneTableData() {
    const { ctx, service } = this;
    const param = ctx.query;
    // console.log(param)
    let results = await service.mysqlTable.getOneTableData(param);
    let db_list = await service.mysqlTable.getAllDataBses();
    results.db_list = db_list;
    // console.log(results)
    // ctx.body = results;
    await this.ctx.render(`/h5${this.ctx.path}`, { results });
  }

}

module.exports = dbcontroller;